package jym.tan.movielibrary.component.ui.table;

import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import jym.tan.movielibrary.component.base.BaseTable;
import jym.tan.movielibrary.constant.ComponentConstant;
import jym.tan.movielibrary.factory.ListenerFactory;
import jym.tan.movielibrary.factory.RendererFactory;

public class MovieSearchResultTable extends BaseTable<MovieSearchResultTable> {

	private static final long serialVersionUID = 45L;

	public MovieSearchResultTable(TableModel tableModel) {
		super(tableModel);
	}

	@Override
	public void initialize() {
		setFillsViewportHeight(true);
		setAutoResizeMode(AUTO_RESIZE_OFF);
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		TableColumnModel tableColumnModel = getColumnModel();
		for (ComponentConstant.ColumnName columnName : ComponentConstant.Column.MOVIE_SEARCH_RESULT_TABLE) {
			int order = columnName.getOrder();
			TableColumn tableColumn = tableColumnModel.getColumn(order);
			if (order == 0) {
				continue;
			}

			tableColumn.setCellRenderer(RendererFactory.getMovieSearchResultTableCellRenderer());
		}

		addMouseMotionListener(this);
		addMouseListener(this);
		getSelectionModel().addListSelectionListener(ListenerFactory.getMovieSearchResultTableSelectionListener());
		getRowSorter().toggleSortOrder(ComponentConstant.ColumnName.RELEASED.getOrder());
	}

	@Override
	public boolean getScrollableTracksViewportWidth() {
		return getPreferredSize().getWidth() < getParent().getWidth();
	}	
}
